const express = require("express");
const router = express.Router();
const mysql = require('mysql')
const db = require('../sql/db')
const $sql = require('../sql/sqlMap')

const connection = mysql.createConnection(db.mysql)
connection.connect()
// 登录验证语句

router.post('/', function (req, res, next) {
    let sql;
    let account = req.body.account;
    let email = account;
    let password = req.body.password;
    let radio = req.body.radio;

    if (radio === 'admin')
        sql = $sql.admin.confirm;
    else if (radio === 'stu')
        sql = $sql.user.confirm;
    else
        sql = $sql.management.confirm;

    connection.query(sql, [account, password, email, password], (err, result) => {
        if (err) {
            console.log(err)
            return
        }
        if (result.length) {
            res.json({
                status: '0',
                msg: '登录成功',
                result: result
            });
        } else {
            res.json({
                status: '1',
                msg: '用户名或密码错误',
                result: ''
            });
        }
    })
});

module.exports = router;
